import { expect } from 'chai';
import childProcess from 'child_process';
import fs from 'node:fs';
import { clearOutput, fileExists } from '../__tests__/__helpers.js';

describe('cliBuildWithJsConfig', () => {
  beforeEach(() => {
    clearOutput(undefined, fs);
  });

  afterEach(() => {
    clearOutput();
  });

  it('should work with json config', () => {
    childProcess.execSync('node ./bin/style-dictionary build --config __tests__/__configs/test.js');
    expect(fileExists('__tests__/__output/web/_icons.css', fs)).to.be.true;
    expect(fileExists('__tests__/__output/android/colors.xml', fs)).to.be.true;
  });

  it('should work with javascript config', () => {
    childProcess.execSync('node ./bin/style-dictionary build --config __tests__/__configs/test.js');
    expect(fileExists('__tests__/__output/web/_icons.css', fs)).to.be.true;
    expect(fileExists('__tests__/__output/android/colors.xml', fs)).to.be.true;
  });

  describe('logging args', () => {
    it('should not log anything if --silent is used', () => {
      const result = childProcess.execSync(
        'node ./bin/style-dictionary build --config __tests__/__configs/test.js --silent',
      );
      expect(result.toString()).to.equal(``);
    });

    it('should only log success logs if --no-warn is used', () => {
      const result = childProcess.execSync(
        'node ./bin/style-dictionary build --config __tests__/__configs/test.js --no-warn',
      );

      expect(result.toString().match('Token collisions detected')).to.be.null;
    });

    it('should log briefly if neither --verbose nor --silent is used', () => {
      //const logStub = stubMethod(console, 'log');
      const result = childProcess.execSync(
        'node ./bin/style-dictionary build --config __tests__/__configs/tokenCollisions.json',
      );
      expect(result.toString()).to.equal(`
css
⚠️ __tests__/__output/css/vars.css
While building vars.css, token collisions were found; output may be unexpected. Ignore this warning if intentional.

Use log.verbosity "verbose" or use CLI option --verbose for more details.
Refer to: https://styledictionary.com/reference/logging/\n`);
    });

    it('should log verbosely if --verbose is used', () => {
      //const logStub = stubMethod(console, 'log');
      const result = childProcess.execSync(
        'node ./bin/style-dictionary build --config __tests__/__configs/tokenCollisions.json --verbose',
      );
      expect(result.toString()).to.equal(`
css
⚠️ __tests__/__output/css/vars.css
While building vars.css, token collisions were found; output may be unexpected. Ignore this warning if intentional.
    Output name red was generated by:
        color.red   #f00
        color.background.red   #f00
This many-to-one issue is usually caused by some combination of:
    * conflicting or similar paths/names in token definitions
    * platform transforms/transformGroups affecting names, especially when removing specificity
    * overly inclusive file filters\n`);
    });
  });
});
